#include "Menu.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

Menu::Menu()
{
}

void Menu::display_main_menu() const
{
    cout << "\n###############################\n";
	cout << "########## MAIN MENU ##########\n";
	cout << "###############################\n\n";
    cout << "\tPress\n\t\t1::Display Info\n\t\t2::Read File\n\t\t3::Show Queries\n\t\t4::Find Queries\n\t\t5::Update Queries\n\t\t6::Exit\n"; //this creates the main menu option cout statements

}
void Menu::display_info() const
{
	cout << "\nThis Restaurant Order Management System was developed by:\n\n\tTeam 5 Members:\n\t\tArchie Anugrah (ITB)\n\t\tRico Balakit (TAMU)\n\t\tTravis Lamb (TAMU)\n\n"; //TTL A.2 Have checked it to Mr.Ward. This code need to be located here. --Archie
}
/*
void Menu::read()
{
	read_orders(orders,"orders.dat");
	read_items(order_items,"orders.dat");
	readIngredientRecipe("recipes.dat"); //Read the ingredient and Recipe data --Archie
	read_menu_items(menu_items,categories,"catmenu.dat"); //this starts up the thing to read catmenu.dat --add category -Archie
	cout << "Reading completed\n";
}
*/

void Menu::read()
{
    int choice;
    string filepath = "";
	cout << "Which file would you like to read and store? Press:\n";
	cout << "\t1 for catmenu.dat (Categories and Menu Items)\n";
	cout << "\t2 for recipes.dat (Ingredients and Recipes)\n";
	cout << "\t3 for orders.dat (Orders and Order Items)\n";
	cout << "\t4 for all (the .dat files must all be in the same folder as this executable)\n";
	cout << "> ";
    cin >> choice;
    if(!cin)
    {
        cerr << "\n\nERROR bad input.\n\n";
        return;
    }
    if(choice>4 || choice<1)
    {
        cerr << "\n\nERROR bad input.\n\n";
        return;
    }
    if(choice != 4)
    {
        cout << "\nWhat is the filepath of the file (EG data/orders.dat)?\nIf the file is in the same folder as this prgram, leave the field blank.\n";
        cout << "> ";
        cin >> filepath;
        if(!cin)
        {
            cerr << "\n\nERROR bad input.\n\n";
            return;
        }
    }

    switch(choice)
    {
        case 1:
            if(filepath == "")
                filepath = "catmenu.dat";
            read_menu_items(menu_items,categories,filepath); //this starts up the thing to read catmenu.dat
            cout << "Done reading file.\n";
            break;
        case 2:
            if(filepath == "")
                filepath = "recipes.dat";
            readIngredientRecipe(filepath); //Read the ingredient and Recipe data --Archie
            cout << "Done reading file.\n";
            break;
        case 3:
         if(filepath == "")
                filepath = "orders.dat";
            read_orders(orders,filepath);
            //uncomment the following to display
            /*for(int i = 0; i < orders.size();i++)
            {
                orders[i].Order::display();
            }*/
            read_items(order_items,filepath);
            //uncomment the following to display
            /*for(int i = 0; i < order_items.size();i++)
            {
                order_items[i].Order_Item::display();
            }*/
            cout << "Done reading file.\n";
            break;
        case 4:
            read_menu_items(menu_items,categories,"catmenu.dat");
            readIngredientRecipe("recipes.dat");
            read_orders(orders,"orders.dat");
            read_items(order_items,"orders.dat");
            cout << "Done reading files.\n";
            break;
    }
}


void Menu::show() const
{
	char UserSelection; //variable to contain User Input
	cout << "How do you want to see the data ? [1=recipe;2=chef;3=price;4=order_ID;5=menu_item;6=top5menu_item;7=orderlargest]\n";
	cin >> UserSelection;

	switch(UserSelection)
	{
		case '1': //recipe 1.a
			ShowFindUpdate::PrintRecipe(recipes,UserSelection);
			break;
		case '2': //chef 1.b -- Archie
			ShowFindUpdate::PrintMenuItemChef(menu_items,recipes); //show, sorted by chef
			break;
		case '3': //price 1.c
			ShowFindUpdate::PrintMenuItemPrice(menu_items);
			break;
		case '4': // C.1.a
			//---Some Code Here---

			break;
		case '5': // C.1.b
			//---Some Code Here---

			break;
		case '6': // C.2.a --Archie
			//Queryarc::DisplayTopFiveMenuItems(order_items,menu_items);
			Queryarc::DisplayTopFiveMenuItems(order_items,menu_items); //display top 5 ordered menu items
			break;
		case '7': // C.2.b --Archie
			Queryarc::DisplayTopOrder(order_items,menu_items); //display top order tab price
			break;
    }

}
void Menu::find() const
{
	char UserSelection; //variable to contain User Input
	cout << "Which total sales you want to see ? [1=table;2=server;3=menu_item]\n";
	cin >> UserSelection;

	switch(UserSelection)
	{
		case '1': //table 2.a -- Rico
			int table_id; //variable to contain user input
			//ask user input
			cout << "Please input the table_id : ";
			cin >> table_id;
			//find
			ShowFindUpdate::TotalTables(orders,order_items,menu_items,table_id);
			break;
		case '2': //server 2.b -- Archie
			int server_id; //variable to contain user input

			//ask user input
			cout << "Please input the server_id : ";
			cin >> server_id;
			//find
			ShowFindUpdate::TotalServer(orders,order_items,menu_items,server_id);
			break;
		case '3': //menu_item 2.c
			int itemid;
			cout << "Please input the menu_item_id : ";
			cin >> itemid;
			ShowFindUpdate::FindMenuItemPrice(itemid, menu_items, order_items);
			break;
    }
}
void Menu::update()
{
	char UserSelection; //variable to contain User Input
	cout << "Which update you want to do ? [1=order_item;2=menu_item;3=category;4=delete order_item;5=delete menu_item]\n";
	cin >> UserSelection;

	// can't use switch case if there's an object initialization
	if (UserSelection == '1') //order_item 3.a
	{
		int order_id; //variable to contain user input
		int menu_item_id;
		int item_quantity;
		int seat_id;
		//ask user input
		cout << "Please input the order_id: ";
		cin >> order_id;
		cout << "Please input the menu_item_id: ";
		cin >> menu_item_id;
		cout << "Please input the quantity of items";
		cin >> item_quantity;
		cout << "Please input the seat_id";
		cin >> seat_id;
        ShowFindUpdate::AddItemByOrder(order_items,menu_items,order_id,menu_item_id,item_quantity, seat_id);
	}
	else if (UserSelection == '2') //menu_item 3.b -- Archie
	{
		int recipe_id; //variable to contain user input
		string category_id;
		string MenuName;
		double price;

		//ask user input
		cout << "Please input the recipe_id : ";
		cin >> recipe_id;
		cout << "Please input the category_id : ";
		cin >> category_id;
		cout << "Please input the Menu Name : ";
		cin >> MenuName;
		cout << "Please input the Price : ";
		cin >> price;

		//update
		ShowFindUpdate::AddMenubyRecipe(menu_items,recipes,recipe_id,category_id,MenuName,price);
	}
	else if (UserSelection == '3') //category 3.c
	{
		categories.push_back(Category(1200,"Soups"));
		cout << "\nNew category \"Soups\" with ID 1200 has been added to categories.";
		categories.push_back(Category(1300,"Salads"));
		cout << "\nNew category \"Salads\" with ID 1300 has been added to categories.";
		categories.push_back(Category(1400,"Sides"));
		cout << "\nNew category \"Sides\" with ID 1400 has been added to categories.";
	}
	else if (UserSelection == '4') //Team problem 1 part C delete order_item
	{
		/*some code here*/
	}
	else if (UserSelection == '5') //Team problem 2 part C delete menu_item
	{
		//variable definition
		string category_id;
		int menu_id;

		//ask user input
		cout << "Menu_item deletion [need category_id and menu_id]\n";
		cout << "Please input the category_id : ";
		cin >> category_id;
		cout << "Please input the menu_id : ";
		cin >> menu_id;

		if (Queryarc::DeleteMenuItemfromCate(menu_items,category_id,menu_id)) //delete the query
		{cout << "Query deleted\n";} //success
		else
		{cout << "Query not found\n";} //fail
			
	}
}

// Read Ingredient Recipe data
// Placed it into ingrdients and recipe vector
// Data Location : filepath to the associated file
// -- Archie
void Menu::readIngredientRecipe(string DataLocation)
{
	//------------Variables dictionary------------

	int Ingr_num; //Total ingredient item
	int Rec_num; //Total recipe item

	//temporary variable to construct ingredient data
	int ingredient_id;
	int recipe_id;
	float amt;
	string units;
	string name;

	//temporary variable to construct recipe data
	int recipe_id2;
	string chef_name;
	string Instruction;

	//------------Algorithm------------

	ingredients.clear(); //clear the current data in ingredients vector
	recipes.clear(); //clear the current data in recipes vector

	//Read the data
	ifstream myfile (DataLocation.c_str()); //Open the data
	if (myfile.is_open())
	{

		/*---------------Ingredient Part-----------------*/
		myfile >> Ingr_num; //read the total ingredient item

		for (int i=0;i<Ingr_num;i++) //get the ingredient data
		{
			myfile >> ingredient_id>> recipe_id>> amt>> units>> name; //parse the line ingredient data
			ingredients.push_back(Ingredient(ingredient_id, recipe_id, amt, units, name));
		}

		/*---------------Recipe Part-----------------*/

		myfile >> Rec_num; //read the total recipe item

		for (int i=0;i<Rec_num;i++) //get the ingredient data
		{

			myfile >>recipe_id2; //read the data
			myfile >>chef_name;

			stringstream tempInstruction; //make temporary stringstream to return the concatenated string
			//Read the instruction text file
			do
			{
				std::getline(myfile, Instruction); //get line and construct the string
				tempInstruction<<Instruction<<'\n';
			} while(Instruction.find('#')==string::npos); //until find # in the line

			recipes.push_back(Recipe(recipe_id2,chef_name,Instructions(tempInstruction.str()))); //add the recipe
		}

		myfile.close();
	}

	else
	{
		cout << "Unable to open \"Recipe and Ingredient\" File"; // if error
	}
}
